import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;

/**
 * @author bigbird-0101
 * @date 2024-03-26 21:59
 */
public class SimpleServerVirthalThread {
    public static void main(String[] args) throws IOException {
        HttpServer httpServer = HttpServer.create(new InetSocketAddress(8989),0);
        httpServer.createContext("/ping", exchange -> {
            OutputStream responseBody = exchange.getResponseBody();
            Headers responseHeaders = exchange.getResponseHeaders();
            responseHeaders.add("Content-Type","application/json");
            String s = "{\"message\":\"pong\"}";
            exchange.sendResponseHeaders(200,s.length());
            responseBody.write(s.getBytes(StandardCharsets.UTF_8));
            responseBody.flush();
            exchange.close();
        });
        httpServer.setExecutor(Executors.newThreadPerTaskExecutor(Thread.ofVirtual().name("vt-",1).factory()));
        httpServer.start();
    }
}
